package seiacpsjava.utilities;

import java.util.concurrent.Callable;

public class MemoryRecorder implements Callable<MemoryStatistics> {
	private static final long DEFAULT_WAIT_TIME = 1000 ; //one second
	private final MemoryStatistics statistics;
	private final long sleepTime;
	private boolean isRecordingStopped = false;

	
	public MemoryRecorder() {
		this.sleepTime = DEFAULT_WAIT_TIME;
		this.statistics = new MemoryStatistics();
	}
	
	public MemoryRecorder(long sleepTime) {
		this.sleepTime = sleepTime;
		this.statistics = new MemoryStatistics();
	}

	public void stopRecording() {
		this.isRecordingStopped = true;
	}
	
	@Override
	public MemoryStatistics call() throws Exception {
		while( isRecordingStopped != true) {
			this.statistics.updateMemoryTotals();
			this.statistics.calculateMemoryRunningAvg();
			Thread.sleep(this.sleepTime);
		}
		return this.statistics;
	}
}
